from tools import cause_damage


def eject_self(self, other, direction):
    if direction == 'horizontal':
        if self.direction.x > 0:  # moving right
            self.rect.right = other.rect.left - 15
        if self.direction.x < 0:  # moving left
            self.rect.left = other.rect.right + 15
        self.rect.centerx = self.rect.centerx
        self.pos.x = self.rect.centerx
    if direction == 'vertical':
        if self.direction.y > 0:  # moving down
            self.rect.bottom = other.rect.top - 15
        if self.direction.y < 0:  # moving up
            self.rect.top = other.rect.bottom + 15
        self.rect.centery = self.rect.centery
        self.pos.y = self.rect.centery
    self.hit_box = self.rect.copy().inflate(-self.rect.width * 0.30, -self.rect.height * 0.30)


def eject_other(self, other, direction):
    if direction == 'horizontal':
        if self.direction.x > 0:  # moving right
            other.rect.right = other.rect.right + 2
        if self.direction.x < 0:  # moving left
            other.rect.left = other.rect.left - 2
        other.rect.centerx = other.rect.centerx
        other.pos.x = other.rect.centerx
    if direction == 'vertical':
        if self.direction.y > 0:  # moving down
            other.rect.bottom = other.rect.bottom + 2
        if self.direction.y < 0:  # moving up
            other.rect.top = other.rect.top - 2
        other.rect.centery = other.rect.centery
        other.pos.y = other.rect.centery
    other.hit_box = other.rect.copy().inflate(-other.rect.width * 0.30, -other.rect.height * 0.30)


def damage(self, other):
    if other.damage:
        self.health -= cause_damage(other.damage, self.defense)
        if other.destruct:
            other.kill()


def waylay(self, collision_sprite, direction):
    if direction == 'horizontal':
        if self.direction.x > 0:  # moving right
            self.hit_box.right = collision_sprite.hit_box.left
        if self.direction.x < 0:  # moving left
            self.hit_box.left = collision_sprite.hit_box.right
        self.rect.centerx = self.hit_box.centerx
        self.pos.x = self.hit_box.centerx

    if direction == 'vertical':
        if self.direction.y > 0:  # moving down
            self.hit_box.bottom = collision_sprite.hit_box.top
        if self.direction.y < 0:  # moving up
            self.hit_box.top = collision_sprite.hit_box.bottom
        self.rect.centery = self.hit_box.centery
        self.pos.y = self.hit_box.centery
